package leetcode;

public class LeetCode7 {

    public static void main(String[] args) {
        System.out.println(Integer.parseInt(new StringBuffer(123 + "").reverse().toString()));
    }

    public int reverse(int x) {
        String s = Math.abs(x) + "";
        StringBuffer buffer = new StringBuffer();
        if (x < 0) {
            buffer.append("-");
        }
        for (int i = s.length() - 1; i >= 0; i--) {
            buffer.append(s.charAt(i));
        }
        try {
            return Integer.parseInt(buffer.toString());
        } catch (Exception e) {
            return 0;
        }
    }

    public int reverse1(int x) {
        String s = new StringBuffer(Math.abs(x) + "").reverse().toString();
        try {
            return Integer.parseInt(s) * (x < 0 ? -1 : 1);
        } catch (Exception e) {
            return 0;
        }
    }
//------------------------------------------------------------------

    public int reverse2(int x) {
        int rev = 0;
        while(x != 0){
            //获取最后一位
            int pop = x % 10;
            x = x / 10;
            if(rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && pop > Integer.MAX_VALUE % 10)){
                rev = 0;
                break;
            }else if(rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && x < Integer.MIN_VALUE % 10)){
                rev = 0;
                break;
            }
            //增加到第一位
            rev = rev * 10 + pop;
        }
        return rev;
    }
}
